// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

package com.netease.yunxin.kit.contactkit.ui.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.netease.yunxin.kit.contactkit.ui.bean.ExtDataBean;
import com.netease.yunxin.kit.common.utils.NetworkUtils;
import com.netease.yunxin.kit.contactkit.ui.R;
import com.netease.yunxin.kit.contactkit.ui.databinding.UserInfoLayoutBinding;
import com.netease.yunxin.kit.contactkit.ui.model.ContactUserInfoBean;
import com.netease.yunxin.kit.contactkit.ui.utils.ColorUtils;

public class ContactInfoView extends FrameLayout {

  private UserInfoLayoutBinding binding;

  private IUserCallback userCallback;

  public ContactInfoView(@NonNull Context context) {
    super(context);
    init(null);
  }

  public ContactInfoView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init(attrs);
  }

  public ContactInfoView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(attrs);
  }

  private void init(AttributeSet attrs) {
    LayoutInflater layoutInflater = LayoutInflater.from(getContext());
    binding = UserInfoLayoutBinding.inflate(layoutInflater, this, true);
  }

  public void setUserCallback(IUserCallback userCallback) {
    this.userCallback = userCallback;
  }

  public void setData(ContactUserInfoBean userInfo) {
    String name = userInfo.data.getUserInfoName();
    String nickName = null;
    if (userInfo.friendInfo != null) {
      nickName = userInfo.friendInfo.getAlias();
    }
    //avatar
    binding.avatarView.setData(
        userInfo.data.getAvatar(), name, ColorUtils.avatarColor(userInfo.data.getAccount()));

    //name
    if (TextUtils.isEmpty(nickName)) {
      binding.tvName.setText(name);
//      binding.tvAccount.setText(
//          String.format(
//              getContext().getString(R.string.contact_user_info_account),
//              userInfo.data.getAccount()));
//      binding.tvCommentName.setVisibility(GONE);
    } else {
      binding.tvName.setText(nickName);
//      binding.tvAccount.setText(
//          String.format(getContext().getString(R.string.contact_user_info_nickname), name));
//      binding.tvCommentName.setText(
//          String.format(
//              getContext().getString(R.string.contact_user_info_account),
//              userInfo.data.getAccount()));
//      binding.tvCommentName.setVisibility(VISIBLE);
    }

    binding.tvBirthday.setText(userInfo.data.getBirthday());
    binding.tvPhone.setText(userInfo.data.getMobile());
    binding.tvEmail.setText(userInfo.data.getEmail());
    binding.tvSignature.setText(userInfo.data.getSignature());

    if (userInfo.isFriend) {
      binding.tvDelete.setText(getResources().getText(R.string.delete_friend));
    } else {
      binding.tvDelete.setText(getResources().getText(R.string.add_friend));
    }
    binding.scBlackList.setOnCheckedChangeListener(null);
    binding.scBlackList.setChecked(userInfo.isBlack);

    binding.scBlackList.setOnCheckedChangeListener(
        (buttonView, isChecked) -> {
          if (!NetworkUtils.isConnected()) {
            Toast.makeText(getContext(), R.string.contact_network_error_tip, Toast.LENGTH_SHORT).show();
            binding.scBlackList.toggle();
            return;
          }
          if (userCallback != null) {
            userCallback.addBlackList(isChecked);
          }
        });
    setIsFriend(userInfo.isFriend);


    //扩展信息
    if (!TextUtils.isEmpty(userInfo.data.getExtension())){
      String json=userInfo.data.getExtension();
      ExtDataBean bean =new Gson().fromJson(json, ExtDataBean.class);
      if (bean!=null){
        if (bean.getIfzhaoshang()!=0){
          binding.imgTag.setVisibility(View.VISIBLE);
          if(bean.getLevel()==0){
            binding.imgTag.setImageResource(R.drawable.attract0);
          }else if(bean.getLevel()==1) {
            binding.imgTag.setImageResource(R.drawable.attract1);
          }else if(bean.getLevel()==2) {
            binding.imgTag.setImageResource(R.drawable.attract2);
          }
          //等级
          if(!TextUtils.isEmpty(bean.getArea())){
            binding.tvAgent.setVisibility(View.VISIBLE);
            binding.tvAgent.setText(bean.getArea()+"服务中心");
          }else{
            binding.tvAgent.setVisibility(View.GONE);
          }

        }
      }
    }else{
      binding.levelBg.setVisibility(View.GONE);
      binding.llTagAgent.setVisibility(View.GONE);
    }




  }

  public void setCommentClickListener(OnClickListener onClickListener) {
    binding.rlyComment.setOnClickListener(onClickListener);
  }

  public void setDeleteClickListener(OnClickListener clickListener) {
    binding.tvDelete.setOnClickListener(clickListener);
  }

  private void setIsFriend(boolean isFriend) {
    if (isFriend) {
      binding.llyFriend.setVisibility(GONE);
      binding.rlyComment.setVisibility(GONE);
      binding.tvChat.setOnClickListener(
          v -> {
            if (userCallback != null) {
              userCallback.goChat();
            }
          });
      binding.tvChat.setText(R.string.chat);
      binding.tvDelete.setVisibility(VISIBLE);
    } else {
      binding.llyFriend.setVisibility(GONE);
      binding.rlyComment.setVisibility(GONE);
      binding.tvChat.setText(R.string.add_friend);
      binding.tvChat.setOnClickListener(
          v -> {
            if (userCallback != null) {
              userCallback.addFriend();
            }
          });
      binding.tvDelete.setVisibility(GONE);
    }
  }

  public void configSCBlackSwitch(int trackRes, int thumbRes) {
    binding.scBlackList.setTrackResource(trackRes);
    binding.scBlackList.setThumbResource(thumbRes);
  }

  public void configAvatarCorner(int cornerSize) {
    binding.avatarView.setCornerRadius(cornerSize);
  }

  public void configChatBtnColor(int color) {
    binding.tvChat.setTextColor(color);
  }

  public void configDivideLineColor(int color) {
    binding.divideLine1.setBackgroundColor(color);
    binding.divideLine2.setBackgroundColor(color);
    binding.divideLine3.setBackgroundColor(color);
  }

  public void configRootBgColor(int color) {
    binding.getRoot().setBackgroundColor(color);
  }

  public void configSignatureMaxLines(int lines) {
    binding.tvSignature.setMaxLines(lines);
  }

  public interface IUserCallback {
    void goChat();

    void addFriend();

    void openMessageNotify(boolean open);

    void addBlackList(boolean add);
  }
}
